﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Collections;


namespace MainClient
{
    public partial class frmTableInfo : Form
    {
        private string strTableName;
        private int oper;

        private bool isTableNameValid = true;
        private bool isTablePriceValid = true;
        
        public const int OPER_ADDTABLE = 1;
        public const int OPER_RMTABLE = 2;
        public const int OPER_UPDTABLE = 3;

        public frmTableInfo(string strTableName, int oper)
        {
            InitializeComponent();
            this.strTableName = strTableName;
            this.oper = oper;
        }

        private void frmTableInfo_Load(object sender, EventArgs e)
        {
            DataAccess.TableDAO tableDAO = new DataAccess.TableDAO();
            Hashtable ht = (Hashtable)tableDAO.getTable(this.strTableName);

            this.txt_tableName.Text = ht["tableName"].ToString().Trim();
            this.txt_tablePrice.Text = this.setMoneyString(double.Parse(ht["tablePrice"].ToString()));
            this.cmb_tableType.SelectedIndex =
                                                        Int32.Parse(ht["tableType"].ToString());
            this.txt_notes.Text = ht["notes"].ToString().Trim();

            switch (this.oper)
            {
                case OPER_ADDTABLE :
                    this.btn_oper.Text = "添加";
                    this.txt_tableName.ReadOnly = false;
                    this.txt_tablePrice.ReadOnly = false;
                    this.cmb_tableType.Enabled = true;
                    this.txt_notes.ReadOnly = false;
                    break;
                case OPER_RMTABLE :
                    this.btn_oper.Text = "移除";
                    this.txt_tableName.ReadOnly = true;
                    this.txt_tablePrice.ReadOnly = true;
                    this.cmb_tableType.Enabled = false;
                    this.txt_notes.ReadOnly = true;
                    break;
                case OPER_UPDTABLE :
                    this.btn_oper.Text = "修改";
                    this.txt_tableName.ReadOnly = true;
                    this.txt_tablePrice.ReadOnly = false;
                    this.cmb_tableType.Enabled = true;
                    this.txt_notes.ReadOnly = false;
                    break;
                default :
                    this.Close();
                    break;
            }
        }

        private void btn_oper_Click(object sender, EventArgs e)
        {
            // Judge whether also have error, if so, do not operation
            if (!isTableNameValid || !isTablePriceValid)
            {
                MessageBox.Show("输入有误，操作失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DataAccess.TableDAO tableDAO = new DataAccess.TableDAO();
            string strShow;
            switch (this.oper)
            {
                case OPER_ADDTABLE:
                    if (tableDAO.isTableNameExist(this.txt_tableName.Text.Trim()))
                    {
                        strShow = "添加新桌位"+this.txt_tableName.Text.Trim()+"失败，原因：该桌位已存在！"; 
                        MessageBox.Show(strShow, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    tableDAO.addNewTable(
                                        LoginUser.getCurrOper(),
                                        this.txt_tableName.Text.Trim(),
                                        this.cmb_tableType.SelectedIndex,
                                        this.getMoneyValue(),
                                        this.txt_notes.Text.Trim(),
                                        DataAccess.TableDAO.STATE_AVAILABLE
                                    );
                    break;
                case OPER_RMTABLE:
                    if (tableDAO.isTableNameExist(this.txt_tableName.Text.Trim()))
                    {
                        tableDAO.deleteOldTable(LoginUser.getCurrOper(), this.txt_tableName.Text.Trim());
                    }
                    else
                    {
                        strShow = "删除桌位" + this.txt_tableName.Text.Trim() + "失败，原因：该桌位不存在！";
                        MessageBox.Show(strShow, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    break;
                case OPER_UPDTABLE:
                    if (tableDAO.isTableNameExist(this.txt_tableName.Text.Trim()))
                    {
                        tableDAO.updateTable(LoginUser.getCurrOper(),
                                                    this.txt_tableName.Text.Trim(),
                                                    this.cmb_tableType.SelectedIndex,
                                                    double.Parse(this.txt_tablePrice.Text),
                                                    this.txt_notes.Text.Trim());
                    }
                    else
                    {
                        strShow = "修改桌位" + this.txt_tableName.Text.Trim() + "失败，原因：该桌位不存在！";
                        MessageBox.Show(strShow, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    break;
                default:
                    this.Close();
                    break;
            }
        } // end of function

        public string setMoneyString(double money)
        {
            if (money > 0d)
                return string.Format("{0:.00}", money);
            else
                return "0.00";
        } // end of function

        private double getMoneyValue()
        {
            //string str = this.txt_tablePrice.Text.Replace(
            //return double.Parse(str);
            
            return 0.0d;
        }

        private void txt_tableName_Validating(object sender, CancelEventArgs e)
        {
            this.errP_tableName.SetError(this.txt_tableName, "");
            if (this.txt_tableName.Text.Trim().Length == 0)
            {
                this.errP_tableName.SetError(this.txt_tableName, "桌位名称不能为空");
                isTableNameValid = false;
                return;
            }
            isTableNameValid = true;
            //if (this.txt_tableName.Text.Trim())
        }

        private void txt_tablePrice_Validating(object sender, CancelEventArgs e)
        {
            this.errP_tablePrice.SetError(this.txt_tablePrice, "");
            Regex regDouble = new Regex(@"^\d+(\.\d+)?$");
            if (!regDouble.Match(this.txt_tablePrice.Text.Trim()).Success)
            {
                this.errP_tablePrice.SetError(this.txt_tablePrice, "包厢费用只允许输入浮点数");
                isTablePriceValid = false;
                return;
            }
            isTablePriceValid = true;
        }

    } // end of class
} // end of namespace
